<?php
/**
 *@file comments.php
 *@author Zhe.Zhou(zhouzhe@haibao.com)
 *@date 2015-7-10 下午12:30:48
 *@brief  
 */
namespace haibao\comment\data;

use haibao\cms\data\fashionwear\FashionWearProperty;
class Comments extends \haibao\comment\data\BaseMysql{
	public function __construct() {
		parent::__construct ( '\haibao\comment\model\data\comments' );
		$this->setConfig( \haibao\classlibrary\www\Config::CLASSLIBRARY_CONFIG_MYSQL_HAIBAO_USER );
	}
	
	public function getArticleCommentsList($articleId,$limit,$page=1){

		$this->setConfig( \haibao\classlibrary\www\Config::CLASSLIBRARY_CONFIG_MYSQL_HAIBAO_CMS );
		$articleSql = '';
		$tag = 0;
		$resArr = array();
		if($articleId){
			$articleSql = "and AP.ArticleId=".$articleId;
			$startLimit = 0;
			$limitNum = 1;
		}else{
			$startLimit = ($page-1)*$limit;
			$limitNum = $limit;
		}
		$sql = "select AP.CommentNum as CN,A.PublishId,A.Title from ArticleProperty AP left join Article A on AP.ArticleId = A.Id where 1 ".$articleSql." order by AP.CommentNum desc limit %s,%s";
		$result = $this->query($sql,array($startLimit,$limitNum));
		while ($result->fetch_assoc()){
			$resArr[$tag]['articleUrl'] = \haibao\classlibrary\comment\Config::getConfig(\haibao\classlibrary\comment\Config::CLASSLIBRARY_CONFIG_HAIBAO_URL)."/article/".$result->getData('PublishId').".htm";
			$resArr[$tag]['commentsNum'] = $result->getData('CN');
			$resArr[$tag]['articleTitle'] = $result->getData('Title');
			$tag++;
		}
		return $resArr;
	}
	
	public function getArticleCommentsPager($articleId,$page){

		$this->setConfig( \haibao\classlibrary\www\Config::CLASSLIBRARY_CONFIG_MYSQL_HAIBAO_CMS );
		$articleSql = '';
		$totalNums = 0;
		if($articleId){
			$articleSql = "and AP.ArticleId=".$articleId;
		}
		$sql = "select count(AP.Id) as Total from ArticleProperty AP left join Article A on AP.ArticleId = A.Id where 1 ".$articleSql;
		$result = $this->query($sql);
		while($result->fetch_assoc()){
			$totalNums = $result->getData('Total');
		}
		return array('totalNums'=>$totalNums,'currentPage'=>$page);
	}

	public function addReportNum($commentId){
		$sql = "update Comments set Report=Report+1 where Id=%s";
		$result = $this->query($sql,array($commentId));
	}
	
	public function addPariseNum($model){
	    $sql = "update Comments set Praise=Praise+1 where Id=%s";
	    $result = $this->query($sql,array($model->CommentId));
	    $this->setConfig( \haibao\classlibrary\www\Config::CLASSLIBRARY_CONFIG_MYSQL_HAIBAO_CMS);
	    // 给不同数据添加评论
	    switch($model->Type){
	    	case \haibao\comment\model\data\CommentType::TYPE_CODE_ARTICLE:
    	        $sql = "update ArticleProperty set CommentPariseNum=CommentPariseNum+1 where ArticleId=%s";	    	        
	    		$this->query($sql,array($model->ArticleId));
	    		break;
	    	case \haibao\comment\model\data\CommentType::TYPE_CODE_PIC:
    	        $sql = "update ImageCollectiveProperty set CommentPariseNum=CommentPariseNum+1 where ImageCollectiveId=%s";
	    		$this->query($sql,array($model->ObjectId));
	    		break;
	    	case \haibao\comment\model\data\CommentType::TYPE_CODE_FEATURE:
    	        $sql = "update FeaturePublish set CommentPariseNum=CommentPariseNum+1 where Id=%s";	    	    
	    		$this->query($sql,array($model->ObjectId));
	    		break;
	    	case \haibao\comment\model\data\CommentType::TYPE_CODE_VIDEO:
    	        $sql = "update VideoProperty set CommentPariseNum=CommentPariseNum+1 where VideoId=%s";
	    		$this->query($sql,array($model->ObjectId));
	    		break;
    		case \haibao\comment\model\data\CommentType::TYPE_CODE_HB_HOME_BLOG:
	    		$this->setConfig( \haibao\classlibrary\comment\Config::CLASSLIBRARY_CONFIG_MYSQL_HAIBAO_DZ3 );
				$sql = "update hb_home_blog set comments_parise_count=comments_parise_count+1 where blogid=%s";
				$this->query($sql,array($model->ObjectId));
    			break;
    		case \haibao\comment\model\data\CommentType::TYPE_CODE_STAR_PK:
    		    $this->setConfig( \haibao\classlibrary\comment\Config::CLASSLIBRARY_CONFIG_MYSQL_HAIBAO_CMS );
		        $sql = "update StarPkProperty set CommentPariseNum=CommentPariseNum+1 where StarPkId=%s";    		    
				$this->query($sql,array($model->ObjectId));
    			break;
    		case \haibao\comment\model\data\CommentType::TYPE_CODE_STAR:
		        $sql = 'update CelebrityRiseCount set CommentPariseNum=CommentPariseNum+1 where CelebrityId=%s';    		    
    			$this->query($sql,array($model->ObjectId));
    			break;
    		case \haibao\comment\model\data\CommentType::TYPE_CODE_BRAND:
		        $sql = 'update BrandStatisticalInfo set CommentPariseNum=CommentPariseNum+1 where BrandId=%s';
    			$this->query($sql,array($model->ObjectId));
    			break;
    		case \haibao\comment\model\data\CommentType::TYPE_CODE_DESIGNER:
		        $sql = 'update DesignerProperty set CommentPariseNum=CommentPariseNum+1 where DesignerId=%s';
    			$this->query($sql,array($model->ObjectId));
    			break;
			case \haibao\comment\model\data\CommentType::TYPE_CODE_MEIJU_INFO:
		        $sql = 'update AmericanProperty set CommentPariseNum=CommentPariseNum+1 where AmericanId=%s';
				$this->query($sql,array($model->ObjectId));
				break;
			case \haibao\comment\model\data\CommentType::TYPE_CODE_IMAGE_LIBRARY:
				$data = new \haibao\cms\data\ImageProperty();
				$data->updateCommentNum($model->ObjectId,false);
				break;
			case \haibao\comment\model\data\CommentType::TYPE_CODE_FASHION_WEAR:
		        $sql = 'update FashionWearProperty set CommentPraiseNum=CommentPraiseNum+1 where FashionWearId=%s';
				$this->query($sql,array($model->ObjectId));
				break;
	    }
	}
	
	public function updateCommentNewNum($objectId,$userId,$commentType,$isQuestion,$isAdd,$isComment,$numInfo){
	    if($isComment){
	        $this->setConfig( \haibao\classlibrary\cms\Config::CLASSLIBRARY_CONFIG_MYSQL_HAIBAO_CMS );
	        if($isAdd){
	            $this->dealCommentNum($objectId,$userId, $commentType, $isQuestion, $isAdd,$isComment);
	        }else{
	            $result =\haibao\comment\business\Comments::getCommentListById($objectId);
	            $userInfo = \haibao\comment\common\Auth::getUserInfo();
	            foreach($result as $value){
	                if($value['IsVerify']){
                        if($value['ReplyCommentId'] == 0 && $value['ReplyNum'] > 0 && $isQuestion == \haibao\comment\model\data\CommentContent::CONTENT_IS_COMMENT){
                            continue;
                        }
	                    $this->dealCommentNum($value['ObjectId'],$userInfo['_auth_user_id'], $value['CommentsTypeCode'], $isQuestion, $isAdd,$isComment,$numInfo);
	                }
	            }
	        }
	    }else{
	        $this->setConfig( \haibao\classlibrary\cms\Config::CLASSLIBRARY_CONFIG_MYSQL_HAIBAO_USER);
	        $model = $objectId;
	        $sql = "update Comments set Praise=Praise+1 where Id=%s";
	        $result = $this->query($sql,array($model->CommentId));
	        $this->setConfig( \haibao\classlibrary\cms\Config::CLASSLIBRARY_CONFIG_MYSQL_HAIBAO_CMS );
	        if($model->Type == \haibao\comment\model\data\CommentType::TYPE_CODE_ARTICLE){
	            $this->dealCommentNum($model->ArticleId,$userId, $model->Type, $isQuestion, true,false);
	        }else{
	            $this->dealCommentNum($model->ObjectId,$userId, $model->Type, $isQuestion, true,false);
	        }
	    }
	}
	
	private function dealCommentNum($objectId,$userId, $commentType, $isQuestion, $isAdd,$isComment,$numInfo = array()){
	    switch ($commentType){
	        case \haibao\comment\model\data\CommentType::TYPE_CODE_ARTICLE:
	           if($isComment){
	                $arrArticleId = \haibao\cms\business\Article::getArticleIdByPublishId(array($objectId));
	                $articleId = array_pop($arrArticleId);
	            }else{
	                $articleId = $objectId;
	            }
	            if($isQuestion == \haibao\comment\model\data\CommentContent::CONTENT_IS_COMMENT){
	                $field = $isComment?'CommentNum':'CommentPariseNum';
	                if(!$isAdd){
	                    $sql = "UPDATE ArticleProperty SET ".$field." =".$field."-1 WHERE ArticleId=%s";
	                }
	            }elseif ($isQuestion == \haibao\comment\model\data\CommentContent::CONTENT_IS_QUESTION){
	                $field = $isComment?'QuestionNum':'QuestionPariseNum';
	                if(!$isAdd){
	                    $sql = "UPDATE ArticleProperty SET QuestionNum = QuestionNum -".$numInfo['num'].",QuestionPariseNum = QuestionPariseNum - ".$numInfo['parise']." WHERE ArticleId=%s";
	                }
	            }
	            if($isAdd){
	                $sql = "UPDATE ArticleProperty SET ".$field."=".$field."+1 WHERE ArticleId=%s";
	            }
	            $result  = $this->query($sql,array($articleId));
	            break;
	        case \haibao\comment\model\data\CommentType::TYPE_CODE_FEATURE:
	            if($isQuestion == \haibao\comment\model\data\CommentContent::CONTENT_IS_COMMENT){
	                $field = $isComment?'CommentCount':'CommentPariseNum';
	                if(!$isAdd){
	                    $sql = 'UPDATE FeaturePublish SET '.$field.'='.$field.'-1 WHERE Id=%s';
	                }
	            }elseif ($isQuestion == \haibao\comment\model\data\CommentContent::CONTENT_IS_QUESTION){
	                $field = $isComment?'QuestionCount':'QuestionPariseCount';
	                if(!$isAdd){
	                    $sql = "UPDATE FeaturePublish SET QuestionCount = QuestionCount -".$numInfo['num'].",QuestionPariseCount = QuestionPariseCount - ".$numInfo['parise']." WHERE Id=%s";
	                }
	            }
	            if($isAdd){
	                $sql = 'UPDATE FeaturePublish SET '.$field.'='.$field.'+1 WHERE Id=%s';
	            }
	            $this->query($sql,array($objectId));
	            break;
	        case \haibao\comment\model\data\CommentType::TYPE_CODE_PIC:
	            if($isQuestion == \haibao\comment\model\data\CommentContent::CONTENT_IS_COMMENT){
	                $field = $isComment?'CommentNum':'CommentPariseNum';
	                if(!$isAdd){
	                    $sql = 'UPDATE ImageCollectiveProperty SET '.$field.'='.$field.'-1 WHERE ImageCollectiveId=%s';
	                }
	            }elseif ($isQuestion == \haibao\comment\model\data\CommentContent::CONTENT_IS_QUESTION){
	                $field = $isComment?'QuestionNum':'QuestionPariseNum';
	                if(!$isAdd){
	                    $sql = "UPDATE ImageCollectiveProperty SET QuestionNum = QuestionNum -".$numInfo['num'].",QuestionPariseNum = QuestionPariseNum - ".$numInfo['parise']." WHERE ImageCollectiveId=%s";
	                }
	            }
	            if($isAdd){
	                $sql = 'UPDATE ImageCollectiveProperty SET '.$field.'='.$field.'+1 WHERE ImageCollectiveId=%s';
	            }
	            $this->query($sql,array($objectId));
	            break;
	        case \haibao\comment\model\data\CommentType::TYPE_CODE_VIDEO:
	            if($isQuestion == \haibao\comment\model\data\CommentContent::CONTENT_IS_COMMENT){
	                $field = $isComment?'CommentNum':'CommentPariseNum';
	                if(!$isAdd){
	                    $sql = 'UPDATE VideoProperty SET '.$field.'='.$field.'-1 WHERE VideoId=%s LIMIT 1';
	                }
	            }elseif ($isQuestion == \haibao\comment\model\data\CommentContent::CONTENT_IS_QUESTION){
	                $field = $isComment?'QuestionNum':'QuestionPariseNum';
	                if(!$isAdd){
	                    $sql = "UPDATE VideoProperty SET QuestionNum = QuestionNum -".$numInfo['num'].",QuestionPariseNum = QuestionPariseNum - ".$numInfo['parise']." WHERE VideoId=%s";
	                }
	            }
	            if($isAdd){
	                $sql = 'UPDATE VideoProperty SET '.$field.'='.$field.'+1 WHERE VideoId=%s LIMIT 1';
	            }
	            $this->query($sql,array($objectId));
	            break;
	        case \haibao\comment\model\data\CommentType::TYPE_CODE_HB_HOME_BLOG:
	            $this->setConfig( \haibao\classlibrary\cms\Config::CLASSLIBRARY_CONFIG_MYSQL_HAIBAO_DZ3 );
	            if($isQuestion == \haibao\comment\model\data\CommentContent::CONTENT_IS_COMMENT){
	                $field = $isComment?'comments_count':'comments_parise_count';
	                if(!$isAdd){
	                    $sql = 'update hb_home_blog set '.$field.'='.$field.'-1 where blogid=%s';
	                }
	            }elseif ($isQuestion == \haibao\comment\model\data\CommentContent::CONTENT_IS_QUESTION){
	                $field = $isComment?'question_count':'question_parise_count';
	                if(!$isAdd){
	                    $sql = "UPDATE hb_home_blog SET question_count = question_count -".$numInfo['num'].",question_parise_count = question_parise_count - ".$numInfo['parise']." WHERE blogid=%s";
	                }
	            }
	            if($isAdd){
	                $sql = 'update hb_home_blog set '.$field.'='.$field.'+1 where blogid=%s';
	            }
	            $this->query($sql,array($objectId));
	            break;
	        case \haibao\comment\model\data\CommentType::TYPE_CODE_STAR_PK:
	            if($isQuestion == \haibao\comment\model\data\CommentContent::CONTENT_IS_COMMENT){
	                $field = $isComment?'CommentNum':'CommentPariseNum';
	                if(!$isAdd){
	                    $sql = 'UPDATE StarPkProperty SET '.$field.'='.$field.'-1 WHERE StarPkId=%s';
	                }
	            }elseif ($isQuestion == \haibao\comment\model\data\CommentContent::CONTENT_IS_QUESTION){
	                $field = $isComment?'QuestionNum':'QuestionPariseNum';
	                if(!$isAdd){
	                    $sql = "UPDATE StarPkProperty SET QuestionNum = QuestionNum -".$numInfo['num'].",QuestionPariseNum = QuestionPariseNum - ".$numInfo['parise']." WHERE StarPkId=%s";
	                }
	            }
	            if($isAdd){
	                $sql = 'UPDATE StarPkProperty SET '.$field.'='.$field.'+1 WHERE StarPkId=%s';
	            }
	            $this->query($sql,array($objectId));
	            break;
	        case \haibao\comment\model\data\CommentType::TYPE_CODE_BRAND:
	            if($isQuestion == \haibao\comment\model\data\CommentContent::CONTENT_IS_COMMENT){
	                $field = $isComment?'CommentsNum':'CommentPariseNum';
	                if(!$isAdd){
	                    $sql = 'UPDATE BrandStatisticalInfo SET '.$field.'='.$field.'-1 WHERE BrandId=%s';
	                }
	            }elseif ($isQuestion == \haibao\comment\model\data\CommentContent::CONTENT_IS_QUESTION){
	                $field = $isComment?'QuestionNum':'QuestionPariseNum';
	                if(!$isAdd){
	                    $sql = "UPDATE BrandStatisticalInfo SET QuestionNum = QuestionNum -".$numInfo['num'].",QuestionPariseNum = QuestionPariseNum - ".$numInfo['parise']." WHERE BrandId=%s";
	                }
	            }
	            if($isAdd){
	                $sql = 'UPDATE BrandStatisticalInfo SET '.$field.'='.$field.'+1 WHERE BrandId=%s';
	                if(!$isComment){
	                    $additionalSql = "insert into BrandAttentionRecord(BrandId,UserId,CreateTime,RecordType) value(%s,%s,%s,%s)";
	                    $this->query($additionalSql,array($objectId,$userId,date('Y-m-d H:i:s'),\haibao\cms\model\data\BrandAttentionRecord::RECORD_TYPE_COMMENT));
	                }
	            }
	            $this->query($sql,array($objectId));
	            break;
	        case \haibao\comment\model\data\CommentType::TYPE_CODE_STAR:
	            if($isQuestion == \haibao\comment\model\data\CommentContent::CONTENT_IS_COMMENT){
	                $field = $isComment?'CommentsCount':'CommentPariseNum';
	                if(!$isAdd){
	                    $sql = 'UPDATE CelebrityRiseCount SET '.$field.'='.$field.'-1 WHERE CelebrityId=%s';
	                }
	            }elseif ($isQuestion == \haibao\comment\model\data\CommentContent::CONTENT_IS_QUESTION){
	                $field = $isComment?'QuestionNum':'QuestionPariseNum';
	                if(!$isAdd){
	                    $sql = "UPDATE CelebrityRiseCount SET QuestionNum = QuestionNum -".$numInfo['num'].",QuestionPariseNum = QuestionPariseNum - ".$numInfo['parise']." WHERE CelebrityId=%s";
	                }
	            }
	            if($isAdd){
	                $sql = 'UPDATE CelebrityRiseCount SET '.$field.'='.$field.'+1 WHERE CelebrityId=%s';
	                if(!$isComment){
	                    $additionalSql = "insert into CelebrityAttentionRecord(CelebrityId,UserId,CreateTime,RecordType) value(%s,%s,%s,%s)";
	                    $this->query($additionalSql,array($objectId,$userId,date('Y-m-d H:i:s'),\haibao\cms\model\data\CelebrityAttentionRecord::RECORD_TYPE_COMMENT));
	                }
	            }
	            $this->query($sql,array($objectId));
	            break;
	        case \haibao\comment\model\data\CommentType::TYPE_CODE_DESIGNER:
	            if($isQuestion == \haibao\comment\model\data\CommentContent::CONTENT_IS_COMMENT){
	                $field = $isComment?'CommentNum':'CommentPariseNum';
	                if(!$isAdd){
	                    $sql = 'update DesignerProperty set '.$field.'='.$field.'-1 where DesignerId=%s';
	                }
	            }elseif ($isQuestion == \haibao\comment\model\data\CommentContent::CONTENT_IS_QUESTION){
	                $field = $isComment?'QuestionNum':'QuestionPariseNum';
	                if(!$isAdd){
	                    $sql = "UPDATE DesignerProperty SET QuestionNum = QuestionNum -".$numInfo['num'].",QuestionPariseNum = QuestionPariseNum - ".$numInfo['parise']." WHERE DesignerId=%s";
	                }
	            }
	            if($isAdd){
	                $sql = 'update DesignerProperty set '.$field.'='.$field.'+1 where DesignerId=%s';
	                if(!$isComment){
	                    $additionalSql = "insert into DesignerAttentionRecord(DesignerId,UserId,CreateTime,RecordType) value(%s,%s,%s,%s)";
	                    $this->query($additionalSql,array($objectId,$userId,date('Y-m-d H:i:s'),\haibao\cms\model\data\designer\DesignerAttentionRecord::RECORD_TYPE_COMMENT));
	                }
	            }
	            $this->query($sql,array($objectId));
	            break;
	        case \haibao\comment\model\data\CommentType::TYPE_CODE_MEIJU_INFO:
	            if($isQuestion == \haibao\comment\model\data\CommentContent::CONTENT_IS_COMMENT){
	                $field = $isComment?'CommentNum':'CommentPariseNum';
	                if(!$isAdd){
	                    $sql = 'UPDATE AmericanProperty SET '.$field.'='.$field.'-1 WHERE AmericanId=%s';
	                }
	            }elseif ($isQuestion == \haibao\comment\model\data\CommentContent::CONTENT_IS_QUESTION){
	                $field = $isComment?'QuestionNum':'QuestionPariseNum';
	                if(!$isAdd){
	                    $sql = "UPDATE AmericanProperty SET QuestionNum = QuestionNum -".$numInfo['num'].",QuestionPariseNum = QuestionPariseNum - ".$numInfo['parise']." WHERE AmericanId=%s";
	                }
	            }
	            if($isAdd){
	                $sql = 'UPDATE AmericanProperty SET '.$field.'='.$field.'+1 WHERE AmericanId=%s';
	            }
	            $this->query($sql,array($objectId));
	            break;
	        case \haibao\comment\model\data\CommentType::TYPE_CODE_IMAGE_LIBRARY:
	            if($isAdd){
	                \haibao\cms\business\ImageProperty::updateCommentNum($objectId,$isComment,$isQuestion);
	            }else{
	                \haibao\cms\business\ImageProperty::subtractCommentNum($objectId,$isComment,$isQuestion,$numInfo);
	            }
	            break;
	        case \haibao\comment\model\data\CommentType::TYPE_CODE_FASHION_WEAR:
	            if($isQuestion == \haibao\comment\model\data\CommentContent::CONTENT_IS_COMMENT){
	                $field = $isComment?'CommentNum':'CommentPraiseNum';
	                if(!$isAdd){
	                    $sql = 'update FashionWearProperty set '.$field.'='.$field.'-1 where FashionWearId=%s';
	                }
	            }elseif ($isQuestion == \haibao\comment\model\data\CommentContent::CONTENT_IS_QUESTION){
	                $field = $isComment? 'QuestionNum':'QuestionPariseNum';
	                if(!$isAdd){
	                    $sql = "UPDATE FashionWearProperty SET QuestionNum = QuestionNum -".$numInfo['num'].",QuestionPariseNum = QuestionPariseNum - ".$numInfo['parise']." WHERE FashionWearId=%s";
	                }
	            }
	            if($isAdd){
	                $sql = 'update FashionWearProperty set '.$field.'='.$field.'+1 where FashionWearId=%s';
	            }
	            $this->query($sql,array($objectId));
	            break;
            case \haibao\comment\model\data\CommentType::TYPE_CODE_GOODS:
                if($isQuestion == \haibao\comment\model\data\CommentContent::CONTENT_IS_COMMENT){
                    $field = $isComment?'CommentNum':'CommentPariseNum';
                    if(!$isAdd){
                        $sql = 'update GoodsStatistic set '.$field.'='.$field.'-1 where GoodsId=%s';
                    }
                }elseif ($isQuestion == \haibao\comment\model\data\CommentContent::CONTENT_IS_QUESTION){
                    $field = $isComment? 'QuestionNum':'QuestionPariseNum';
                    if(!$isAdd){
                        $sql = "UPDATE GoodsStatistic SET QuestionNum = QuestionNum -".$numInfo['num'].",QuestionPariseNum = QuestionPariseNum - ".$numInfo['parise']." WHERE GoodsId=%s";
                    }
                }
                if($isAdd){
                    $sql = 'update GoodsStatistic set '.$field.'='.$field.'+1 where GoodsId=%s';
                }
                $this->query($sql,array($objectId));
                break;
	    }
	}
	
	public function addReplyNum($commentId){
		$sql = "update Comments set ReplyNum=ReplyNum+1 where Id=%s";
		$result = $this->query($sql,array($commentId));
	}
	
	public function getTitleInfo($article,$feature,$pic,$video,$star,$american){

		$this->setConfig( \haibao\classlibrary\www\Config::CLASSLIBRARY_CONFIG_MYSQL_HAIBAO_CMS );
	    $articleTitleArray = array();
	    $featureTitleArray = array();
	    $picTitleArray = array();
	    $videoTitleArray = array();
	    $starTitleArray = array();
	    $americanTitleArray = array();
	    
	    if($article){
	        $articles = preg_replace('/(\d+)/', '%s', implode(',', $article));
	        $sql = "select Id,Title,PublishId from `Article` where Id in (".$articles.")";
	        $resultArticle = $this->query($sql, $article);
	        while($row =$resultArticle->fetch_assoc()){
        	       $articleTitleArray[$row['Id']] = $row;
        	   }
	    }
	    if($feature){
	        $features = preg_replace('/(\d+)/', '%s', implode(',', $feature));
	        $sql = "select t1.Id,t2.FeatureName,t1.Url from `FeaturePublish` t1 inner join FeatureDefine t2 on t1.FeatureDefineId=t2.Id where t1.Id in (".$features.")";
	        $resultFeature = $this->query($sql, $feature);
	        while($row =$resultFeature->fetch_assoc()){
	            $featureTitleArray[$row['Id']] = $row;
	        }
	    }
	    
	    if($pic){
	        $pics = preg_replace('/(\d+)/', '%s', implode(',', $pic));
	        $sql = "select Id,Title from `ImageCollective` where Id in (".$pics.")";
	        $resultPic = $this->query($sql, $pic);
	        while($row =$resultPic->fetch_assoc()){
	            $picTitleArray[$row['Id']] = $row;
	        }
	    }
	    
	    if($video){
	        $videos = preg_replace('/(\d+)/', '%s', implode(',', $video));
	        $sql = "select Id,Title,VideoUrl  from `Video` where Id in (".$videos.")";
	        $resultVideo = $this->query($sql, $video);
	        while($row =$resultVideo->fetch_assoc()){
	            $videoTitleArray[$row['Id']] = $row;
	        }
	    }
	    
	    if($star){
	    	$stars = preg_replace('/(\d+)/', '%s', implode(',', $star));
	    	$sql = "select Id,Title  from `StarPkDefine` where Id in (".$stars.")";
	    	$resultStar = $this->query($sql, $star);
	    	while($row = $resultStar->fetch_assoc()){
	    		$starTitleArray[$row['Id']] = $row;
	    	}
	    }
	    
	    if($american){
	    	$americans = preg_replace('/(\d+)/', '%s', implode(',', $american));
	    	$sql = "select Id,NameCH from `AmericanDefine` where Id in (".$americans.")";
	    	$resultAmerican = $this->query($sql, $american);
	    	while($row = $resultAmerican->fetch_assoc()){
	    		$americanTitleArray[$row['Id']] = $row;
	    	}
	    }
	    
	    return array(
	        'articleTitle'=>$articleTitleArray,
	        'featureTitle'=>$featureTitleArray,
	        'picTitle'=>$picTitleArray,
	        'videoTitle'=>$videoTitleArray,
	        'starTitle'=>$starTitleArray,
	        'americanTitle'=>$americanTitleArray
	    );
    }	

	public function getSensitiveWords($type = 1){
		
		$data = array();
		$this->setConfig( \haibao\classlibrary\comment\Config::CLASSLIBRARY_CONFIG_MYSQL_HAIBAO_DZ3 );
		$sql = "SELECT id,find FROM hb_common_word Where type = %s order by id desc";
		$result = $this->query($sql,array($type));
		$getVideoTitle = array();
		while($row = $result->fetch_assoc()){
			$data[$row['id']] = $row['find'];
		}
		return $data;
	}
	public function getReplyNameList($myCommentReplyIdList){
	    $replyIds = preg_replace('/(\d+)/', '%s', implode(',', $myCommentReplyIdList));
	    $sql = "select * from `Comments` where UserId in (".$replyIds.") GROUP BY UserId";
	    $result = $this->query($sql, $myCommentReplyIdList);
	    $returnReplyUserName = array();
	    while ($row = $result->fetch_assoc()){
	        $returnReplyUserName[$row['UserId']] = $row['UserNick'];
	    }
	    return $returnReplyUserName;
	}
	
	public function hotCommentList($commentType,$channel){
	    
	    if(isset($channel)){
            $sql = "SELECT count(*) AS count,Id,ObjectId,UserId,MyComment,CommentsTypeCode,UserNick,CreateTime,Channel FROM Comments WHERE CommentsTypeCode = %s AND Channel = %s GROUP BY ObjectId ORDER BY count DESC LIMIT 10;";
            $result = $this->query($sql,array($commentType,$channel));
	    }else{
	       $sql = "SELECT count(*) AS count,Id,ObjectId,UserId,MyComment,CommentsTypeCode,UserNick,CreateTime,Channel FROM Comments  WHERE CommentsTypeCode = %s GROUP BY ObjectId ORDER BY count DESC LIMIT 10;";
	       $result = $this->query($sql,array($commentType));
	       
	    }
	    $returnHotComment = array();
	    while ($row = $result->fetch_assoc()){
	        $returnHotComment[$row['ObjectId']] = $row;
	    }
	    return $returnHotComment;
	} 
	
	public function verifyComment($ids){
	    $str = preg_replace('/(\d+)/', '%s', implode(',', $ids));
	    $sql = "update Comments set IsVerify=1 where Id in (".$str.")";
	    $this->query($sql,$ids);
	    return true;
	}
	
	public function sensitiveComment($ids){
	    $str = preg_replace('/(\d+)/', '%s', implode(',', $ids));
	    $sql = "update Comments set IsSensitive=1 where Id in (".$str.")";
	    $this->query($sql,$ids);
	    return true;
	}
	
	public function getVestStatisticList($queryArr,$isEditorSta = true){
	    $sql = "select CreateBy,CommentsTypeCode,COUNT(Id) as VestNum FROM Comments";
	    if(!$isEditorSta){
	       $sql = "select CreateBy,CommentsTypeCode,COUNT(Id) as VestNum FROM Comments";
	    }
	    $sql .= " where IsBack=1";
	    $sql .= " And CreateBy<>477";
	    if(!empty($queryArr['createBy'])){
	        if(is_array($queryArr['createBy'])){
	            $sql .= " And CreateBy in (".implode(',', $queryArr['createBy']).")";
	        } else {
	            $sql .= " And CreateBy = ".$queryArr['createBy'];
	        }
	    }
	    if(!empty($queryArr['commentType'])){
	        $sql .= " And CommentsTypeCode = '".$queryArr['commentType']."'";
	    }
	    if(!empty($queryArr['publishTimeType'])){
	        $publishTimeType = (int)$queryArr['publishTimeType'];
	        switch ($publishTimeType){
	            case 1:
	                $sql .= " And CreateTime >= '".date('Y-m-d', strtotime('-1 day'))."' And CreateTime <= '".date('Y-m-d',time())."'";
	                break;
	            case 2:
	                $sql .= " And CreateTime >= '".date('Y-m-d', strtotime('-7 day'))."' And CreateTime <= '".date('Y-m-d',time())."'";
	                break;
	            case 3:
	                $sql .= " And CreateTime >= '".date('Y-m-d', strtotime('-30 day'))."' And CreateTime <= '".date('Y-m-d',time())."'";
	                break;
	        }
	    }
	    
	    if(!empty($queryArr['startDate'])){
	        $sql .= " And CreateTime >= '".$queryArr['startDate']."'";
	    }
	    
	    if(!empty($queryArr['endDate'])){
	        $sql .= " And CreateTime <= '".$queryArr['endDate']."'";
	    }
	    
	    $vestList = array();
	    $VestNum = 0;
	    if($isEditorSta){
	        $sql .= " GROUP BY CreateBy";
	    }
	    $result = $this->query($sql);
	    
	    if(!$isEditorSta){
	        while($result->fetch_assoc()){
	            $VestNum = $result->getData('VestNum');
	        }
	    }else{
	        while($result->fetch_assoc()){
	            $VestNum = $VestNum + $result->getData('VestNum');
	        }
	        $limit='';
	        if(!isset($queryArr['source'])){
	            $limit = " Limit 0,20";
	            if(!empty($queryArr['page']) && $queryArr['page'] > 1){
	                $startCount = ($queryArr['page'] - 1) * 20 + 1;
	                $limit = " Limit ".$startCount.",20";
	            }
	        }
	        $sql .= $limit;
	        $result = $this->query($sql);
	        $k = 0;
	        while($result->fetch_assoc()){
	            $vestList[$k]['UserId']=$result->getData('CreateBy');
	            $vestList[$k]['VestNum']=$result->getData('VestNum');
	            $k++;
	        }
	    }
	    
	    return array('List'=>$vestList,'VestNum'=>$VestNum);
	}

	/**
     * 合并数据
     */
    public function mergeUserData($userId,$unRegUserId,$nickname){
    	$this->setConfig( \haibao\classlibrary\comment\Config::CLASSLIBRARY_CONFIG_MYSQL_HAIBAO_USER );
    	$sql = "UPDATE `Comments` set UserId = %s,UserNick = %s,UnregUserId=null where UnregUserId = %s";
    	$this->query($sql,array($userId,$nickname,$unRegUserId));
    }
}